package com.xiaoyg.lock;

import org.openjdk.jcstress.annotations.*;
import org.openjdk.jcstress.infra.results.I_Result;

/**
 * 有依赖关系的时候不能重排序
 * 写后读：
 * int a = 1;
 * int b = a;
 * 写后写：
 * int a = 1;
 * int a = 2;
 * 读后写：
 * int a = 1;
 * int a = 2;
 * int b = a;
 * 以上无法重排序
 */
@JCStressTest
@Outcome(id = {"1","4"},expect = Expect.ACCEPTABLE,desc = "ok")
@Outcome(id = {"0"},expect = Expect.ACCEPTABLE_INTERESTING,desc = "danger")
@State
public class TestOrderliness {

    int num =0;
    boolean ready = false;

    @Actor
    public void actor1(I_Result r){
        if (ready) {
            r.r1 = num + num;
        }else {
            r.r1 = 1;
        }
    }

    @Actor
    public void actor2(I_Result r){
        num = 2;
        ready = true;
    }

}
